﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;
using BusinessLogic;

namespace Manager
{
    public partial class ServiceManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Load_Data();
            }
        }

        #region Load Data
        private void Load_Data()
        {
            DataTable dtbService = ServiceData.SelectAll();
            if (dtbService != null && (dtbService.Rows.Count > 0))
            {
                rptService.DataSource = dtbService;
                rptService.DataBind();
            }
        }

        protected void rptService_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int serviceID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ServiceID"));
            Label lblStatus = (Label)e.Item.FindControl("lblStatus");
            Label lblDescription = (Label)e.Item.FindControl("lblDescription");
            ServiceInfo serviceInfo = ServiceData.SelectByServiceID(serviceID);

            string description = serviceInfo.ServiceDescription;
            if (description.Length > 200)
            {
                lblDescription.Text = description.Substring(0, 200) + "...";
            }
            else
            {
                lblDescription.Text = description;
            }

            if (serviceInfo.ServiceStatus == true)
            {
                lblStatus.Text = "Hiển thị";
            }
            else
            {
                lblStatus.Text = "Không Hiển thị";
            }
        }
        #endregion

        protected void rptService_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int serviceID = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "View")
            {
                Response.Redirect("ViewService.aspx?ID=" + serviceID);
            }

            if (e.CommandName == "Del")
            {
                ServiceData.DeleteByServiceID(serviceID);
                Response.Redirect("ServiceManager.aspx");
            }

            if (e.CommandName == "Lock")
            {
                ServiceBusiness serviceBusiness = new ServiceBusiness();
                serviceBusiness.UpdateStatus(serviceID);
                Load_Data();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddService.aspx");
        }
    }
}